package com.gewu.pm.ui.activity.filter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.LogUtils;
import com.gewu.pm.R;
import com.gewu.pm.aop.SingleClick;
import com.gewu.pm.app.AppActivity;
import com.gewu.pm.ui.adapter.FilterOrderStatusAdapter;
import com.gewu.pm.ui.dialog.SelectSalesRegionDialog;
import com.gewu.pm.ui.dialog.SelectSalespersonDialog;
import com.gewu.pm.ui.dialog.SelectSubmitterDialog;
import com.gewu.pm.ui.dialog.bean.SalespersonBean;
import com.hjq.base.BaseAdapter;
import com.hjq.base.BaseDialog;

import java.util.Arrays;
import java.util.List;

/**
 * 销售订单筛选页面
 */
public final class OldFilterSalesActivity extends AppActivity implements BaseAdapter.OnItemClickListener {

    private static final String TAG = "FilterSalesActivity";

    /**
     * 订单状态
     */
    private RecyclerView rvOrderStatus;
    private FilterOrderStatusAdapter orderStatusAdapter;

    /**
     * 终端
     */
    private TextView tvTerminal;

    /**
     * 配送商
     */
    private TextView tvDistributor;

    /**
     * 业务员
     */
    private TextView tvSalesman;

    /**
     * 提交人
     */
    private TextView tvSubmitter;

    /**
     * 销售区域
     */
    private TextView tvSalesArea;

    /**
     * 底部按钮
     */
    private TextView tvReset;
    private TextView tvConfirm;

    /**
     * 订单状态选项列表
     */
    private List<String> orderStatusOptions;

    public static void start(Context context) {
        Intent intent = new Intent(context, OldFilterSalesActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.filter_sales_activity;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        // 订单状态
        rvOrderStatus = findViewById(R.id.rv_order_status);

        // 终端
        tvTerminal = findViewById(R.id.tv_terminal);

        // 配送商
        tvDistributor = findViewById(R.id.tv_distributor);

        // 业务员
        tvSalesman = findViewById(R.id.tv_salesman);

        // 提交人
        tvSubmitter = findViewById(R.id.tv_submitter);

        // 销售区域
        tvSalesArea = findViewById(R.id.tv_sales_area);

        // 底部按钮
        tvReset = findViewById(R.id.tv_reset);
        tvConfirm = findViewById(R.id.tv_confirm);

        // 设置RecyclerView
        setupOrderStatusRecyclerView();

        // 设置点击事件
        setOnClickListener(
                tvTerminal, tvDistributor, tvSalesman, tvSubmitter, tvSalesArea,
                tvReset, tvConfirm);
    }

    @Override
    protected void initData() {
        // 初始化订单状态选项
        orderStatusOptions = Arrays.asList(
                "已审批",
                "部分发货",
                "未发货关闭",
                "全部发货关闭",
                "部分发货关闭",
                "已取消"
        );
        orderStatusAdapter.setData(orderStatusOptions);
    }

    /**
     * 设置订单状态RecyclerView
     */
    private void setupOrderStatusRecyclerView() {
        // 设置网格布局，3列
        GridLayoutManager layoutManager = new GridLayoutManager(this, 3);
        rvOrderStatus.setLayoutManager(layoutManager);

        // 设置适配器
        orderStatusAdapter = new FilterOrderStatusAdapter(this);
        orderStatusAdapter.setOnItemClickListener(this);
        rvOrderStatus.setAdapter(orderStatusAdapter);
    }
    @SingleClick
    @Override
    public void onClick(View view) {
        int viewId = view.getId();
        if (viewId == R.id.tv_terminal) {// 点击终端
            new SelectSubmitterDialog.Builder(this,102)//102 = 只显示终端
                    .setListener(new SelectSubmitterDialog.OnListener() {
                        @Override
                        public void onSelected(BaseDialog dialog, String submitter, int type, Object data) {
                            // 显示选中的提交人
                            tvTerminal.setText(submitter);
                            tvTerminal.setTextColor(getResources().getColor(R.color.cb3));
                        }
                    })
                    .show();

        } else if (viewId == R.id.tv_distributor) {// 点击配送商
            new SelectSubmitterDialog.Builder(this,101)//101 = 只显示配送商
                    .setListener(new SelectSubmitterDialog.OnListener() {
                        @Override
                        public void onSelected(BaseDialog dialog, String submitter, int type, Object data) {
                            // 显示选中的提交人
                            tvDistributor.setText(submitter);
                            tvDistributor.setTextColor(getResources().getColor(R.color.cb3));
                        }
                    })
                    .show();

        } else if (viewId == R.id.tv_salesman) {
            // 点击业务员
            new SelectSalespersonDialog.Builder(this)
                    .setListener(new SelectSalespersonDialog.OnListener() {
                        @Override
                        public void onSelected(BaseDialog dialog, String name, SalespersonBean salesperson) {
                            // 显示选中的业务员
                            tvSalesman.setText(name);
                            tvSalesman.setTextColor(getResources().getColor(R.color.cb3));
                        }
                    })
                    .show();

        } else if (viewId == R.id.tv_submitter) {// 点击提交人，弹出选择对话框
            new SelectSubmitterDialog.Builder(this)
                    .setListener(new SelectSubmitterDialog.OnListener() {
                        @Override
                        public void onSelected(BaseDialog dialog, String submitter, int type, Object data) {
                            // 显示选中的提交人
                            tvSubmitter.setText(submitter);
                            tvSubmitter.setTextColor(getResources().getColor(R.color.cb3));
                        }

                        @Override
                        public void onCancel(BaseDialog dialog) {
                            // 取消选择
                        }
                    })
                    .show();

        } else if (viewId == R.id.tv_sales_area) {
            // 点击销售区域，弹出选择对话框
            new SelectSalesRegionDialog.Builder(this)
                    .setDefaultRegion(tvSalesArea.getText().toString())
                    .setListener(new SelectSalesRegionDialog.OnListener() {
                        @Override
                        public void onSelected(BaseDialog dialog, String region) {
                            // 显示选中的区域
                            tvSalesArea.setText(region);
                            tvSalesArea.setTextColor(getResources().getColor(R.color.cb3));
                        }

                        @Override
                        public void onCancel(BaseDialog dialog) {
                            // 取消选择
                        }
                    })
                    .show();

        } else if (viewId == R.id.tv_reset) {
            // 点击重置按钮
            resetAllSelections();

        } else if (viewId == R.id.tv_confirm) {
            // 点击确定按钮
            confirmFilter();
        }
    }

    /**
     * 重置所有选择
     */
    private void resetAllSelections() {
        // 重置订单状态
        orderStatusAdapter.clearSelection();

        // 重置终端
        tvTerminal.setText(getString(R.string.please_select_terminal));
        tvTerminal.setTextColor(getResources().getColor(R.color.cb9));

        // 重置配送商
        tvDistributor.setText(getString(R.string.please_select_distributor));
        tvDistributor.setTextColor(getResources().getColor(R.color.cb9));

        // 重置业务员
        tvSalesman.setText(getString(R.string.please_select_salesman));
        tvSalesman.setTextColor(getResources().getColor(R.color.cb9));

        // 重置提交人
        tvSubmitter.setText(getString(R.string.please_select_submitter));
        tvSubmitter.setTextColor(getResources().getColor(R.color.cb9));

        // 重置销售区域
        tvSalesArea.setText(getString(R.string.please_select_sales_area));
        tvSalesArea.setTextColor(getResources().getColor(R.color.cb9));

        toast("已重置");
    }

    /**
     * 确定筛选，打印所有选中的内容
     */
    private void confirmFilter() {
        StringBuilder result = new StringBuilder();

        // 订单状态
        String orderStatus = orderStatusAdapter.getSelectedText();
        if (!orderStatus.isEmpty()) {
            result.append("订单状态: ").append(orderStatus).append("\n");
        }

        // 终端
        String terminal = tvTerminal.getText().toString();
        if (!terminal.equals(getString(R.string.please_select_terminal))) {
            result.append("终端: ").append(terminal).append("\n");
        }

        // 配送商
        String distributor = tvDistributor.getText().toString();
        if (!distributor.equals(getString(R.string.please_select_distributor))) {
            result.append("配送商: ").append(distributor).append("\n");
        }

        // 业务员
        String salesman = tvSalesman.getText().toString();
        if (!salesman.equals(getString(R.string.please_select_salesman))) {
            result.append("业务员: ").append(salesman).append("\n");
        }

        // 提交人
        String submitter = tvSubmitter.getText().toString();
        if (!submitter.equals(getString(R.string.please_select_submitter))) {
            result.append("提交人: ").append(submitter).append("\n");
        }

        // 销售区域
        String salesArea = tvSalesArea.getText().toString();
        if (!salesArea.equals(getString(R.string.please_select_sales_area))) {
            result.append("销售区域: ").append(salesArea).append("\n");
        }

        // 打印所有选中的内容
        LogUtils.d(TAG, "========== 销售订单筛选条件 ==========");
        if (result.length() == 0) {
            LogUtils.d(TAG, "未选择任何筛选条件");
            toast("未选择任何筛选条件");
        } else {
            LogUtils.d(TAG, result.toString());
            toast("已确定筛选，详情请查看日志");
            // TODO: 将筛选条件返回给上一个页面或执行筛选操作
        }
        LogUtils.d(TAG, "==============================");
    }

    /**
     * {@link BaseAdapter.OnItemClickListener}
     */
    @Override
    public void onItemClick(RecyclerView recyclerView, View itemView, int position) {
        int viewId = recyclerView.getId();

        if (viewId == R.id.rv_order_status) {
            // 订单状态点击（多选）
            orderStatusAdapter.toggleSelection(position);
            // 移除 Toast 提示，让点击反馈更流畅
            // 用户可以通过视觉效果（红色边框和角标）直接看到选中状态
        }
    }
}

